package pumpkin.ast;

import pumpkin.evt.Environment;
import pumpkin.word.TokenSource;

/**
 * T->FT'
 * 
 * @author jiang
 *
 */
public class Texpr implements Node {

	Fexpr fexpr =null;
	Texprp texprp = null;

	@Override
	public void build(TokenSource tokenSource) {
		fexpr = new Fexpr();
		texprp = new Texprp();
		fexpr.build(tokenSource);
		texprp.build(tokenSource);
	}

	@Override
	public Object excute(Environment env) {
		Object left=fexpr.excute(env);
		Object right=texprp.excute(env);
		if(left==null) {
			return right;
		}
		if(right==null) {
			return left;
		}
		char c = texprp.getOerator();
		if (c == '*') {
			if (left instanceof Double || right instanceof Double) {
				return (Double.parseDouble(left.toString()) * Double.parseDouble(right.toString()));
			} else {
				return (Integer.parseInt(left.toString()) * Integer.parseInt(right.toString()));
			}
		} else if (c == '/') {
			if (left instanceof Double || right instanceof Double) {
				return (Double.parseDouble(left.toString()) / Double.parseDouble(right.toString()));
			} else {
				return (Integer.parseInt(left.toString()) / Integer.parseInt(right.toString()));
			}
		}
		return null;
	}

}
